package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("UrlController")
public class UrlController extends CmiEmaController {

	@RequestMapping("/listUrls")
	public ModelAndView listUrls(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = urlPaginationService.updatePagination(request, pagination);
		Set<Url> urls = urlPaginationService.getPaginationUrls(pagination);

		mav.addObject("urls", urls);
		mav.addObject("entityName", "url");
		mav.addObject("pagination", pagination);
		mav.setViewName("url/listUrls.jsp");
		return mav;
	}

	public String listUrls() {
		return "redirect:/listUrls";
	}

	@RequestMapping("/viewUrl")
	public ModelAndView viewUrl(@RequestParam Integer urlId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("url", urlDAO.findUrlByPrimaryKey(urlId));
		mav.setViewName("url/viewUrl.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteUrls")
	public ModelAndView confirmDeleteUrls(
			@RequestParam(value = "deleteUrlsIds", required = true) Set<Integer> deleteUrlsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Url> deleteUrls = urlDAO.findUrlsByIds(deleteUrlsIds);
		Set<Url> referencedUrls = urlService.loadReferenceUrls(deleteUrls);
		boolean hasReferenced;
		if (referencedUrls.size() == 0) {
			mav.addObject("deleteEntitys", deleteUrls);
			hasReferenced = false;
		} else {
			deleteUrls.removeAll(referencedUrls);
			mav.addObject("referencedEntitys", referencedUrls);
			mav.addObject("deleteEntitys", deleteUrls);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Url");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listUrls");
		return mav;
	}

	@RequestMapping("/deleteUrl")
	public String deleteUrl(@RequestParam Integer urlId) {
		Url url = urlDAO.findUrlByPrimaryKey(urlId);
		urlService.deleteUrl(url);
		return "forward:/listUrls";
	}

	@RequestMapping("/confirmDeleteUrl")
	public ModelAndView confirmDeleteUrl(@RequestParam Integer urlId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("url", urlDAO.findUrlByPrimaryKey(urlId));

		mav.setViewName("url/deleteUrl.jsp");

		return mav;
	}

	@RequestMapping("/deleteUrls")
	public String deleteUrls(@RequestParam(value = "deleteUrlsId", required = true) Set<Integer> deleteUrlsId) {
		urlService.deleteUrls(deleteUrlsId);
		return "forward:/listUrls";
	}

	@RequestMapping("/editUrl")
	public ModelAndView editUrl(@RequestParam Integer urlId) {

		ModelAndView mav = new ModelAndView();

		Url url = urlDAO.findUrlByPrimaryKey(urlId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("url", url);

		// 把所有的引用url对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(urlService.loadUrlObjectReferencedSet(url));

		// 将编辑页面url添加到mav中
		mav.setViewName("url/editUrl.jsp");
		return mav;
	}

	@RequestMapping("/newUrl")
	public ModelAndView newUrl() {
		ModelAndView mav = new ModelAndView();
		Url url = new Url();

		mav.addObject("url", url);

		// 把所有的引用url对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(urlService.loadUrlObjectReferencedSet(url));

		mav.addObject("newFlag", true);
		mav.setViewName("url/editUrl.jsp");

		return mav;
	}

	@RequestMapping("/saveUrl")
	public ModelAndView saveUrl(@ModelAttribute Url url) {

		ModelAndView mav = new ModelAndView();

		Site siteTemp = url.getSite();
		Site site = siteTemp == null ? null : CmiHibernateProxyUtil.deproxy(siteDAO.findSiteById(siteTemp.getId()));
		User authorTemp = url.getAuthor();
		User author = authorTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(userDAO.findUserById(authorTemp.getId()));

		Url existingUrl = CmiHibernateProxyUtil.deproxy(urlDAO.findUrlByPhysicalPrimaryKey(url.getValue()));

		url.setSite(site);
		url.setAuthor(author);
		// 新建记录
		if (url.getId() == null) {
			if (url.equals(existingUrl)) {

				mav.addAllObjects(urlService.loadUrlClassReferencedSet(url));
				// mav = editContinue(mav, url);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("url/editUrl.jsp");
			} else {
				urlService.saveUrl(url);
				mav.setViewName("forward:/listUrls");
			}
		}
		// 编辑记录
		else {
			Url originUrl = CmiHibernateProxyUtil.deproxy(urlDAO.findUrlByPrimaryKey(url.getId()));
			if (!url.equals(originUrl) && url.equals(existingUrl)) {
				mav.addAllObjects(urlService.loadUrlClassReferencedSet(url));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("url/editUrl.jsp");
			} else {
				urlService.saveUrl(url);
				mav.setViewName("forward:/listUrls");
			}
		}
		return mav;
	}

	@RequestMapping("/urlController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
